package com.wc.算法提高课.C第三章_图论.最小生成树的扩展应用.秘密的牛奶运输;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/17 11:02
 * @description https://www.acwing.com/problem/content/description/1150/
 */
public class Main {
    /**
     * 思路：<p>
     * 次小生成树, 一定是最小生成树替换了一条边<p>
     * sum + a - b, a是最小生成树中的边, b是非最小生成树中的边, 要使得min(sum + a - b), b需要更大一点<p>
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 510, M = 10010;
    // 存储最小生成树
    static int[] h = new int[N], e = new int[N << 1], ne = new int[N << 1], w = new int[N << 1];
    // 存储所有边
    static int[][] edges = new int[M][4];
    // dist1 查找最小生成树中连接a, b中的当中的最大边权,  dist2 查找最小生成树中连接a, b中的次大边权
    static int[][] dist1 = new int[N][N], dist2 = new int[N][N];
    static int[] p = new int[N];
    static int n, m, idx = 1;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) p[i] = i;
        for (int i = 1; i <= m; i++) {
            int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
            edges[i][0] = a;
            edges[i][1] = b;
            edges[i][2] = c;
        }
        Arrays.sort(edges, 1, m + 1, Comparator.comparingInt(o -> o[2]));
        long sum = 0;
        for (int i = 1; i <= m; i++) {
            int a = edges[i][0], b = edges[i][1], c = edges[i][2];
            int pa = find(a), pb = find(b);
            if (pa != pb) {
                p[pa] = pb;
                edges[i][3] = 1;
                add(a, b, c);
                add(b, a, c);
                sum += c;
            }
        }
        for (int i = 1; i <= n; i++) dfs(i, -1, 0, -1, dist1[i], dist2[i]);
        long res = (long) 1e18;
        for (int i = 1; i <= m; i++) {
            if (edges[i][3] == 0) {
                int a = edges[i][0], b = edges[i][1], c = edges[i][2];
                long t = (long) 1e18;
                if (c > dist1[a][b]) t = Math.min(t, sum + c - dist1[a][b]);
                else if (c == dist1[a][b] && dist2[a][b] > 0 && c > dist2[a][b])
                    t = Math.min(t, sum + c - dist2[a][b]);
                res = Math.min(t, res);
            }
        }
        out.println(res);
        out.flush();
    }

    static void dfs(int u, int fa, int maxd1, int maxd2, int[] dist1, int[] dist2) {
        dist1[u] = maxd1;
        dist2[u] = maxd2;
        for (int i = h[u]; i > 0; i = ne[i]) {
            int j = e[i];
            if (j == fa) continue;
            int td1 = maxd1, td2 = maxd2;
            if (w[i] > td1) {
                td2 = td1;
                td1 = w[i];
                // 这个要严格次小
            } else if (w[i] < td1 && w[i] > td2) td2 = w[i];
            dfs(j, u, td1, td2, dist1, dist2);
        }
    }

    static int find(int x) {
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }

    static void add(int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}


class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}